import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.io.File;

/**
 * Custom JPanel extension to allow for easy background image implementation.
 * Simply create instance of class and use image name as parameter.
 * 
 * Make sure to put the image in the "Images" folder of the current project
 * directory.
 * 
 * @author Shiv Patel
 * @version v1.0
 * 
 */
@SuppressWarnings("serial")
public class BackgroundPanel extends JPanel implements java.io.Serializable {

    private transient Image image;

    /**
     * Constructor for BackgroundPanel class
     * 
     * @param filename
     *            background filename with extension
     */
    public BackgroundPanel(String filename) {
        String url = "Images/" + filename;
        try {
            File file = new File(url);
            this.image = ImageIO.read(file);
        } catch (Exception e) {
        }
    }

    /**
     * Background helper function
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            ImageObserver observer = null;
            g.drawImage(image, 0, 0, observer);
        }
    }

}
